package cc.sunni;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiangli
 * @since 2021/1/7 10:35
 */
public class GuavaTest {
    // 连接器,常用的方法是跳过NULL元素：skipNulls()  /  对于NULL元素使用其他替代：useForNull(String)
    private static final Joiner JOINER = Joiner.on(",").skipNulls();
    // 分割器,常用的方法是：trimResults()/omitEmptyStrings()
    private static final Splitter SPLITTER = Splitter.on(",").trimResults().omitEmptyStrings();

    public static void main(String[] args) {
        // 把集合/数组中的元素join
        String join = JOINER.join(Lists.newArrayList("a", null, "c"));
        System.out.println("join=" + join);

        for (String tmp : SPLITTER.split("  a,   ,  b,,")) {
            System.out.println("|" + tmp + "|");
        }

        String strWorld = "wer|dfd|dd|dfd|dda|de|dr";
        String[] words = strWorld.split("\\|");
        List<String> wordList = new ArrayList<String>();
        for (String word : words) {
            wordList.add(word);
        }
        Multiset<String> wordsMultiset = HashMultiset.create();
        wordsMultiset.addAll(wordList);

        for (String key : wordsMultiset.elementSet()) {
            System.out.println(key + " count：" + wordsMultiset.count(key));
        }
    }
}
